package com.xjtu.trade.service.impl;


import com.xjtu.trade.mapper.OrderItemMapper;
import com.xjtu.trade.mapper.OrderMapper;
import com.xjtu.trade.mapper.ProductMapper;
import com.xjtu.trade.pojo.Order;
import com.xjtu.trade.pojo.OrderExample;
import com.xjtu.trade.pojo.OrderItem;
import com.xjtu.trade.pojo.OrderItemExample;
import com.xjtu.trade.pojo.Product;
import com.xjtu.trade.pojo.ProductExample;
import com.xjtu.trade.pojo.User;
import com.xjtu.trade.service.OrderItemService;
import com.xjtu.trade.service.OrderService;
import com.xjtu.trade.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    UserService userService;
    @Autowired
    OrderItemService orderItemService;

    @Override
    public void add(Order c) {
        orderMapper.insert(c);
    }

    @Override
    public void delete(int id) {
        orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Order c) {
        orderMapper.updateByPrimaryKeySelective(c);
    }

    @Override
    public Order get(int id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,rollbackForClassName="Exception")
    public float add(Order o, List<OrderItem> ois) {
        float total = 0;
        add(o);

        if(false)
            throw new RuntimeException();

        for (OrderItem oi: ois) {
            oi.setOid(o.getId());
            orderItemService.update(oi);
            total+=oi.getProduct().getPromotePrice()*oi.getNumber();
        }
        return total;
    }

    public List<Order> list(){
        OrderExample example =new OrderExample();
        example.setOrderByClause("id desc");
        List<Order> orders = orderMapper.selectByExample(example);
        setUser(orders);
        return orders;
    }

    @Override
    public List list(int uid, String excludedStatus) {
        OrderExample example =new OrderExample();
        example.createCriteria().andUidEqualTo(uid).andStatusNotEqualTo(excludedStatus);
        example.setOrderByClause("id desc");
        return orderMapper.selectByExample(example);
    }
    
    @Override
	public java.util.List list(int uid) {
		ProductExample example=new ProductExample();
		example.createCriteria().andUidEqualTo(uid);
		example.setOrderByClause("id desc");
		List<Product> products=productMapper.selectByExample(example);
		List<Integer> pids=new ArrayList<>();
		for(Product p:products) {
			pids.add(p.getId());
		}
		List<Integer> oids=new ArrayList<>();
		
		for(int pid:pids) {
			OrderItemExample example1=new OrderItemExample();
			example1.createCriteria().andPidEqualTo(pid);
			example1.setOrderByClause("id desc");
			List<OrderItem> OrderItems=orderItemMapper.selectByExample(example1);
			for(OrderItem oi:OrderItems) {
				if(oi.getOid()!=null)
					oids.add(oi.getOid());
			}
		}
		List<Order> orders =new LinkedList<>();
		for(Integer k:oids) {
			orders.add(orderMapper.selectByPrimaryKey(k));
		}

		return orders;
	}

    ;

    public void setUser(List<Order> os){
        for (Order o : os)
            setUser(o);
    }
    public void setUser(Order o){
        int uid = o.getUid();
        User u = userService.get(uid);
        o.setUser(u);
    }
}
